<!DOCTYPE html>
<html>
<head>
    <title>设备功能测试</title>
    <meta name="viewport" content="initial-scale=1, maximum-scale=1, minimum-scale=1, user-scalable=no"
          charset="UTF-8"/>
    <style type="text/css">
        * {
            margin: 0;
            padding: 0;
        }

        .box {
            text-align: center;
        }

        .btn {
            line-height: 24px;
            text-align: center;
            font-size: 14px;
            border: 1px solid blue;
            background-color: #fff;
            margin-bottom: 20px;
            padding: 0 10px;
        }

        .message {
            position: fixed;
            bottom: 0;
            width: 100%;
            padding: 10px;
            font-size: 14px;
            color: #000;
        }
    </style>
    <script src="../js/autoQR.js"></script>
    <script src="../js/jquery.min.js"></script>
    <script src="../js/HKNativeJSBridge.js?=v0.2"></script>

</head>
<body>
<div class="box">
    <div class="message" id="message"></div>
    <br/>
    <button class="btn" onclick="testCamera()">相机</button>
    <br/>
    <button class="btn" onclick="testAlbum()">相册</button>
    <br/>
    <button class="btn" onclick="testLocation()">位置信息</button>
    <br/>
    <button class="btn" onclick="tesGethttp()">http-get接口</button>
    <br/>
    <button class="btn" onclick="tesPosthttp()">http-post接口</button>
    <br/>
    <button class="btn" onclick="testAjaxGet()">http-Ajax-Get接口</button>
    <br/>
    <button class="btn" onclick="testAjaxPost()">http-Ajax-Post接口</button>
    <br/>
    <button class="btn" onclick="testQRCodeScan()">二维码扫描</button>
    <br/>
    <img id="imgID" style="width: 200px;height: 200px" src="">

    <!--    <img style='width:200px;height:200px;' src='data:image/png;base64,"+basedata+"'>;-->


    <!--    <img src="-->
</div>

<script>

    function testCamera() {

        hkNativeDevice.camera({edit: true, wantFilePath: true,wantBase64Data:true},photoResult);
    }

    function photoResult(obj) {

        <!--    <img src="-->

        if (obj.base64) {
            let src = "data:image/" + obj.type + ";base64," + obj.base64;

            let img = document.getElementById('imgID');

            img.src = src;
        } else if (obj.filePath) {
            let img = document.getElementById('imgID');

            //图片路径需要和当前html在同一个目录下
            img.src = obj.filePath;

            document.getElementById('message').innerHTML = result;
        }

        // console.log("recv" + result);
    }

    function testAlbum() {

        hkNativeDevice.album({edit: true, wantFilePath: true,wantBase64Data:true},function (obj) {

            // let obj = eval('(' + result + ')');

            <!--    <img src="-->

            if (obj.base64) {
                let src = "data:image/" + obj.type + ";base64," + obj.base64;

                let img = document.getElementById('imgID');

                img.src = src;
            } else if (obj.filePath) {
                let img = document.getElementById('imgID');

                //图片路径需要和当前html在同一个目录下
                img.src = obj.filePath;

                document.getElementById('message').innerHTML = result;
            }

        });

    }

    function testLocation() {

        hkNativeDevice.location({wantAddress: true,type:0,wantContinuous:false},function (message) {

            let str = JSON.stringify(message); //返回字符串

            document.getElementById('message').innerHTML = "locaiton:" + str;
        });
    }

    function tesGethttp() {

        hkNativeHttp.get({
            url: "http://" + window.location.host + "/request",
            headers: {"Content-Type":"application/json"},
            params: {id: "2", pwd: "val2"}
        },function (message) {

            let str = JSON.stringify(message); //返回字符串

            document.getElementById('message').innerHTML = "http get response:" + str;
        })

    }

    function testAjaxGet() {

        // LBXHookAjax();

        let url = 'https://tcc.taobao.com/cc/json/mobile_tel_segment.htm?tel=15852509988';
        //跨域服务器也需要配置支持才可以
        url = "http://192.168.0.110:8080/request?id=1&pwd=val1";
        // url = "http://192.168.0.106:8080/request";

        $.ajax(url, {

            //jsonp 服务器返回json格式数据,不设置也是json对象，如果设置了，oc编写的json， 提示parse fail
            dataType: 'jsonp',
            crossDomain: true,//跨域
            jsonp: 'callback',
            type: 'get',//HTTP请求类型
            timeout: 10000,//超时时间设置为10秒；
            async: true,
            success: function (data) {
                if (data) {
                    var str = JSON.stringify(data);
                    document.getElementById('message').innerHTML = "http get response:" + str;
                    // console.log(str);
                }
            },
            error: function (xhr, type, errorThrown) {
                console.log("ajax  error:" + type);

                document.getElementById('message').innerHTML =  "ajax get error:" + type;
                //alert(xhr);
            }
        });
    }

    function testAjaxPost() {

        let url = 'https://tcc.taobao.com/cc/json/mobile_tel_segment.htm?tel=15852509988';
        //跨域服务器也需要配置支持才可以
        url = "http://192.168.0.110:8080/request";


        $.ajax(url, {
            data: {
                "id":"1",
                "pwd":"val1"
            },
            //jsonp 服务器返回json格式数据,不设置也是json对象，如果设置了，oc编写的json， 提示parse fail
            dataType: 'jsonp',
            crossDomain: true,//跨域
            jsonp: 'callback',
            type: 'POST',//HTTP请求类型
            timeout: 10000,//超时时间设置为10秒；
            async:true,
            cache:false,
            contentType: "application/json",
            success: function (data) {
                if (data) {
                    var str = JSON.stringify(data);
                    document.getElementById('message').innerHTML = "http post response:" + str;
                }
            },
            error: function (xhr, type, errorThrown) {
                console.log("ajax  error:" + type);

                document.getElementById('message').innerHTML =  "ajax post on error:" + type;
                //alert(xhr);
            }
        });
    }

    function tesPosthttp() {

        hkNativeHttp.post({
            url: "http://" + window.location.host + "/request",
            headers: {"Content-Type":"application/json"},
            params: {id: "1", pwd: "val1"}
        },function (message) {
            let str = JSON.stringify(message); //返回字符串

            document.getElementById('message').innerHTML = "http post response:" + str;
        })
    }

    function testQRCodeScan() {

        hkNativeScan.barCode({type:0},function (message) {
            // let str = JSON.stringify(message); //返回字符串

            document.getElementById('message').innerHTML = "scan result:" + message.data;
        })
    }



    console.log(window.location.host);
    console.log(window.location.href);

</script>
</body>
</html>
